﻿(function () {
    layui.use(["layer", "jquery", "form", "laydate"], function () {
        var layer = layui.layer;
        var $ = layui.jquery;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;

        $(document).ready(function () {
            $("#btnSearch").click(onSearchBtnClick);
        });

        var currentDate = moment().format("YYYY-MM-DD");
        if (moment().get("hours") < 15) {
            currentDate = moment().add(-1, "days").format("YYYY-MM-DD");
        }
        laydate.render({
            elem: '#txtSearchDate',
            value: currentDate,
            isInitValue: true,
            showBottom: false
        });

        function onSearchBtnClick() {
            var loadingIndex = layer.msg(addStyle("正在加载 ......"),
                { icon: 16, shade: 0.01, offset: ["25%"], time: false });
            $.ajax("/Order/QueryPremiumOrder", {
                method: "POST",
                data: "date=" + $("#txtSearchDate").val(),
                complete: function (jqXHR, textStatus) {
                    layer.close(loadingIndex);
                },
                success: function (data, textStatus, jqXHR) {
                    if (data.Success == true) {
                        handleData(data.Data);
                    } else {
                        layer.msg(addStyle("加载数据失败: " + data.Msg),
                            { icon: 2, time: 3 * 1000, offset: ["25%"], closeBtn: true }
                        );
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg(addStyle("系统错误，请联系管理员！"),
                        { icon: 2, time: 3 * 1000, offset: ["25%"], closeBtn: true });
                }
            });

            function handleData(data) {
                layer.open({
                    type: 1,
                    title: "满堂创品",
                    shade: [0.1, '#000'],
                    skin: 'layui-layer-molv',
                    area: ["90%", "95%"],
                    resize: false,
                    content: '<div id="PremiumOrderDlg2">' + $("#PremiumOrderDlg").html() + '</div>',
                    btn: ["确定", "打印"],
                    btn1: function (index, layero, that) {
                        layer.close(index);
                    },
                    btn2: function (index, layero, that) {
                        layer.close(index);
                        var html = '<table id="Table2">';
                        for (var i = 0; i < data.length; i++) {
                            var item = data[i];
                            if ((i + 1) % 4 == 1) {
                                html += '<tr>';
                            }
                            html += '<td>' + item["BuyerName"] + ': ' + item["Amount"].toFixed(2) + '</td>';
                            if ((i + 1) % 4 == 0 || i == data.length - 1) {
                                html += '</tr>';
                            }
                        }
                        html += '</table>';
                        layer.open({
                            type: 1,
                            title: "满堂创品",
                            shade: [0.1, '#000'],
                            skin: 'layui-layer-molv',
                            area: ["90%", "95%"],
                            resize: false,
                            content: '<div id="PrintPremiumOrderDlg2">' + html + '</div>',
                            btn: ["打印", "关闭"],
                            btn1: function (index, layero, that) {
                                printJS({
                                    printable: "Table2",
                                    type: "html"
                                });
                            },
                            btn2: function (index, layero, that) {
                                layer.close(index);
                            }
                        });
                    }
                });

                var el = $("#PremiumOrderDlg2 .premiumOrderDlgList > ul").eq(0);
                var html = '';
                data.forEach(item => {
                    html += '<li tagUserId="' + item["BuyUserId"] + '">' + item["BuyerName"] + ': ' + item["Amount"].toFixed(2) + '</li>';
                });
                html += '';
                el.append(html);
            }
        }

        function addStyle(str) {
            return '<span style="font-size: 16px;">' + str + '</span>';
        }
    });
})();